package com.sl.au.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.sl.au.entity.Account;
import com.sl.au.entity.User;

public interface AccountRepository extends JpaRepository<Account, String> {
	Account findByAccountNameAndPassword(String name, String password);

	Account findByAccountName(String name);

	/// 一个账号的数量
	@Query("select count(a) from Account a where a.accountName=?1")
	long getAccountCount(String accountName);

	@Query("select a from Account a where a.user.id=?")
	Account getByUserId(String userId);

	/**
	 * 删除指定用户的权限账号
	 * 
	 * @param user
	 * @return
	 */
	Long deleteByUser(User user);

	/**
	 * 快速查询
	 * 
	 * @param key
	 * @return
	 */
	@Query("select a from Account a where a.user.name like %?1% or a.role.name like %?1%")
	Page<Account> getByKey(String key, Pageable pageable);

	Account findByUser(User user);

	/**移动端获取信息分配的职员
	 * @param chanceId
	 * @return
	 */
	/*@Query("select a from Account a where (a.user not in (select c.user from ChanceManagerDelivery c  where c.saleChance.id =?1 )) and a.user.saleArea.areaName=?2")
	List<Account>findFreeStaff(String chanceId,String areaName);*/
	
	@Query("select a from Account a where (a.accountName=?1 and a.password=?2) or (a.user.contactType=?1 and a.password=?2)")
	Account findByAccountNameOrPhoneNumberAndPassword(String name, String password);
	
	@Query("select a from Account a where a.user.id=?1 and a.role.name=?2")
	Account findByUserAndRoleName(String userId, String roleName);
}